﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using SnippetBox.Core.Common.Extensions;
using SnippetBox.Core.Data.Models;
using SnippetBox.Core.Data.Service;
using SnippetBox.Core.Infrastructure;

namespace SnippetBox.Web.Areas.Manage.Controllers
{
    [Authorize(Roles = Roles.ADMIN)]
    public class SettingsController : Controller
    {
        public ActionResult Index() {
            Settings s = Settings.Current;
            return View(s);
        }        
        
        [HttpPost]
        public ActionResult Index(Settings settings) {
            if (!ModelState.IsValid)
                return View(settings);
            
            UpdateModel(Settings.Current);
            Settings.Current.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult Load() {
            Settings.Current.LoadChanges();
            return Content("Loaded");
        }        
        
        public ActionResult Set() {
            Settings.Current.General = new GeneralSettings();
            Settings.Current.General.SiteTitle = "Hello " + DateTime.Now.Second;
            Settings.Current.SaveChanges();

            return Content("Loaded");
        }

    }


}
